<?php

/* TODO: Add code here */
/**
 * Lớp đối tượng của FAQ phụ trách tương tác với cơ sở dữ liệu và những phương thức khác liên quan.
 * 
 **/
class user_section
{
	#region user_common
	const ACT_ADD							= 10;
	const ACT_UPDATE						= 11;
	const ACT_DELETE						= 12;
	const ACT_CHANGE_PAGE					= 13;
	const ACT_SHOW_EDIT = 14;
	const NUM_PER_PAGE = 10;
	
	const TBL_T_SECTION			= 't_section';
	
	const SQL_INSERT_SECTION			= 'INSERT INTO `{0}`
												(`section_id`,`name`,`detail`)
												VALUES (\'{1}\',\'{2}\',\'{3}\');';
	const SQL_UPDATE_SECTION			= 'UPDATE `{0}`
														SET 
														  `name` = \'{1}\',
														  `detail` = \'{2}\'
														WHERE `section_id` = \'{3}\';';
	
	const SQL_CREATE_TABLE_T_SECTION		= 'CREATE TABLE `{0}` (
												  `section_id` bigint(20) NOT NULL COMMENT \'Mã ngành hàng tăng tự động\',
												  `name` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT \'Tên ngành hàng\',
												  `detail` text COLLATE utf8_unicode_ci COMMENT \'Thông tin thêm về ngành hàng\',
												  `delete_flag` tinyint(4) DEFAULT NULL COMMENT \'0: avaiable,1 : delete\',
												  PRIMARY KEY (`section_id`)
												) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
	#end region
	
	
	#region Variables
	var $_objConnection;
	#end region
	
	
	#region Contructors
	/**
	*  Phương thức khởi tạo đối tượng faq đồng thời tạo connection đến db
	*
	* @param object $objConnection Đối tượng kết nối đến db
			
	* @return void 
	*
	*/
	public function user_section($objConnection)
	{
		$this->_objConnection = $objConnection;
		
	}
	#end region
	
	
	#region Private Functions
	
	#end region
	
	
	#region Public Functions
	public function insert($name,$detail) 
	{
		$intID = user_common::getMaxID(self::TBL_T_SECTION);
		$strSQL = user_common::prepareQuery(self::SQL_INSERT_SECTION,
				array(self::TBL_T_SECTION,$intID,user_common::escape_mysql_string($name),user_common::escape_mysql_string($detail) ));
		$strTableName = self::TBL_T_SECTION;
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_SECTION,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add section:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	
	/**
	 * Thêm mới một nhà sản xuất
	 *
	 * @param int $m_section_id Mã nhà sản xuất
	 * @param int $m_cat_id Mã nhóm
	 * @param string $name Tên sản phẩm
	 * @param string $m_image Tên file hình ảnh
	 * @return bool True->Thành công, False->thất bại
	 *
	 */
	public function update($intID,$name,$detail) 
	{
		$strSQL = user_common::prepareQuery(self::SQL_UPDATE_SECTION,
				array(self::TBL_T_SECTION,user_common::escape_mysql_string($name),user_common::escape_mysql_string($detail),$intID ));
		
		if (!user_common::ExecutequeryWithCheckExistedTable($strSQL,self::SQL_CREATE_TABLE_T_SECTION,$this->_objConnection,$strTableName))
		{
			//echo $strSQL;
			user_common::writeLog('Error add section:'.$strSQL,1);
			return false;
		}	
		return $intID;
		
	}
	
	
	public function getsectionByID($m_id,$selectField='*') 
	{
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_SECTION ,' where `section_id`=\''.$m_id.'\' '));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult[0])
		{
			user_common::writeLog('getsectionByID:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult[0];
	}
	public function getListsection($intPage, $where)
	{
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE,array('*',
					self::TBL_T_SECTION,' WHERE delete_flag IS NULL or delete_flag=\'0\' ORDER BY `name` ASC  limit '.(($intPage-1)* self::NUM_PER_PAGE).','.self::NUM_PER_PAGE));
		
		$arrsection = $this->_objConnection->selectCommand($strSQL);
		//print_r($arrsection);
		$strHTML = ' <table class="tbl-list">
				<thead>
				<td>Tên</td>
				<td>Mô tả</td>
				<td>Display</td>
				<td class="lastCell"></td>
				</thead>
				<tbody>';
		$icount = count($arrsection);
		for($i=0;$i<$icount;$i++)
		{
			$strHTML.='<tr class="'.($i%2==0?'even':'odd').'">
					<td>'.$arrsection[$i]['name'].'</td>
					<td>'.$arrsection[$i]['detail'].'</td>
					<td><input type="checkbox" onclick="objArticleMn.showHideArticle(this)" '.($arrsection['delete_flag']?'':'checked=checked').' /></td>
					<td class="lastCell"><b><a href="javascript:_objsection.showEdit(\''.$arrsection[$i]['section_id'].'\',this);">Edit</a></b></td>
					</tr>';
		}
		$strHTML.='</tbody></table>';
		
		$strHTML .= "<div>".user_common::getPagingHTMLByNum($intPage,self::NUM_PER_PAGE,user_common::getTotalRecord(self::TBL_T_SECTION,$this->_objConnection),
				"_objsection.changePage")."</div>";
		return $strHTML;
	}
	
	
	/**
	 * Hàm lấy tất cả record của c_doc_xxxx dựa vào danh sách doc_id truyền vào dùng cho việc search.
	 *	
	 * @param string $selectField Cho biết các field cần lấy.
	 * @return array Danh sách record 
	 * @author ThanhViet added [20101005]
	 */
	public function getAllSection($selectField='*') 
	{
		/*$arrDocID = Array();
		foreach($listID as $item)
		{
			$arrDocID[$item['id']] = $item['id'];
		}*/
		//echo '<br>count:'.count($listID);
		
		$strSQL .= user_common::prepareQuery(user_common::SQL_SELECT_FREE, array($selectField, self::TBL_T_SECTION ,' order by name'));
		//echo '<br>SQL:'.$strSQL;
		$arrResult =$this->_objConnection->selectCommand($strSQL);
		//print_r($arrResult);
		//echo '<br>';
		if(!$arrResult)
		{
			user_common::writeLog('getAllSection:'.$strSQL,1,$_mainFrame->pPage);
			return null;
		}	
		//print_r($arrResult);
		return $arrResult;
	}
	
	/**
	 * This is method outputHTMLSection select option
	 *
	 * @param mixed $arrFaq This is a description
	 * @param mixed $strKeyword This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function outputHTMLSection($arrSection,$bLoadSect=1)
	{
		if($bLoadSect)
		{
			$strHTML = '<select id="txtSect" name="txtSect" value="" style="width: 50%;"  onchange="_objAdminProduct.loadCatalogue()">';
		}
		else
			{
			$strHTML = '<select id="txtSect" name="txtSect" value="" style="width: 50%;" >';
			
				}
		$strHTML .= '<option value="0">--Chọn ngành hàng--</option>';
		
		foreach($arrSection as $item)
		{
			$strHTML .= '<option value="'.$item['section_id'].'">'.$item['name'].'</option>';
		}
		$strHTML .= '</select>';
		return $strHTML;
	}
	
	
	
	#end region
}

?>